import Foundation

// 移动方向
enum Direction: String, CaseIterable {
    case forward = "前进"
    case backward = "后退"
    case left = "左转"          // 保持兼容性，实际代表左旋转
    case right = "右转"         // 保持兼容性，实际代表右旋转
    case leftShift = "左平移"   // 新增：左平移
    case rightShift = "右平移"  // 新增：右平移
    case stop = "停止"
    case horn = "鸣笛"          // 新增：鸣笛指令
}

// 速度级别
enum SpeedLevel: Int, CaseIterable {
    case stop = 0
    case slow = 1
    case medium = 2
    case fast = 3
    case turbo = 4
    
    var description: String {
        switch self {
        case .stop:
            return "停止"
        case .slow:
            return "慢速"
        case .medium:
            return "中速"
        case .fast:
            return "快速"
        case .turbo:
            return "极速"
        }
    }
    
    var value: Double {
        return Double(self.rawValue) / Double(SpeedLevel.turbo.rawValue)
    }
}

// 鸣笛参数
struct HornParameters {
    let status: UInt8         // 鸣笛状态：0x01开启，0x00关闭
    let duration: TimeInterval // 持续时间（秒），支持0.05-2.0秒范围
    
    init(status: UInt8 = 0x01, duration: TimeInterval = 0.1) {
        self.status = status
        // 限制持续时间在合理范围内：0.05秒到2.0秒
        self.duration = max(0.05, min(2.0, duration))
    }
    
    // 计算硬件delayLevel（将duration转换为0x01-0x14的级别）
    var delayLevel: UInt8 {
        // 将0.05-2.0秒映射到1-20级别
        let normalizedDuration = (duration - 0.05) / (2.0 - 0.05) // 0.0-1.0
        let level = Int(normalizedDuration * 19) + 1 // 1-20
        return UInt8(max(1, min(20, level)))
    }
    
    // 预定义鸣笛模式（保持向后兼容）
    static let short = HornParameters(status: 0x01, duration: 0.05)       // 极短鸣笛
    static let medium = HornParameters(status: 0x01, duration: 0.15)      // 短鸣笛
    static let long = HornParameters(status: 0x01, duration: 0.3)         // 中等鸣笛
    static let beat = HornParameters(status: 0x01, duration: 0.1)         // 节拍鸣笛
    static let rhythm = HornParameters(status: 0x01, duration: 0.2)       // 节奏鸣笛
    
    // 新增：更多音乐相关的预定义
    static let tick = HornParameters(status: 0x01, duration: 0.06)        // 滴答声（极短）
    static let quarter = HornParameters(status: 0x01, duration: 0.25)     // 四分音符
    static let half = HornParameters(status: 0x01, duration: 0.5)         // 二分音符
    static let whole = HornParameters(status: 0x01, duration: 1.0)        // 全音符
    static let sustained = HornParameters(status: 0x01, duration: 1.5)    // 持续音
    
    // 根据音乐节拍生成鸣笛参数
    static func forBeat(duration: TimeInterval) -> HornParameters {
        return HornParameters(status: 0x01, duration: duration)
    }
}

// 车辆控制命令
struct CarControlCommand {
    var direction: Direction
    var speed: SpeedLevel
    var timestamp: Date
    var duration: TimeInterval? // 可选，指定持续时间
    var hornParameters: HornParameters? // 新增：鸣笛参数（仅当direction为.horn时有效）
    
    init(direction: Direction, speed: SpeedLevel, timestamp: Date = Date(), duration: TimeInterval? = nil, hornParameters: HornParameters? = nil) {
        self.direction = direction
        self.speed = speed
        self.timestamp = timestamp
        self.duration = duration
        self.hornParameters = hornParameters
    }
    
    // 停止命令
    static var stop: CarControlCommand {
        return CarControlCommand(direction: .stop, speed: .stop)
    }
    
    // 鸣笛命令便捷构造方法
    static func horn(parameters: HornParameters = .beat, duration: TimeInterval = 0.1) -> CarControlCommand {
        return CarControlCommand(direction: .horn, speed: .stop, duration: duration, hornParameters: parameters)
    }
    
    // 常用鸣笛命令
    static var shortBeep: CarControlCommand {
        return CarControlCommand.horn(parameters: .short, duration: 0.1)
    }
    
    static var beatBeep: CarControlCommand {
        return CarControlCommand.horn(parameters: .beat, duration: 0.2)
    }
    
    static var rhythmBeep: CarControlCommand {
        return CarControlCommand.horn(parameters: .rhythm, duration: 0.3)
    }
}

// 车辆连接状态
enum ConnectionStatus: String {
    case connected = "已连接"
    case disconnected = "未连接"
    case connecting = "连接中"
    case error = "连接错误"
    
    var isConnected: Bool {
        return self == .connected
    }
}

// 相机操作
enum CameraOperation: String {
    case takePicture = "拍照"
    case startRecording = "开始录像"
    case stopRecording = "停止录像"
    case pan = "平移"
    case tilt = "倾斜"
    case zoomIn = "放大"
    case zoomOut = "缩小"
}

// 车辆状态
struct CarStatus {
    var connectionStatus: ConnectionStatus
    var batteryLevel: Double // 0.0-1.0
    var currentSpeed: Double // 实际速度，0.0-1.0
    var isRecording: Bool
    var isCameraActive: Bool
    var lastCommand: CarControlCommand?
    var lastUpdated: Date
    
    // 格式化的电量
    var batteryString: String {
        return "\(Int(batteryLevel * 100))%"
    }
    
    // 格式化的速度
    var speedString: String {
        return "\(Int(currentSpeed * 100))%"
    }
}

// 示例数据
extension CarStatus {
    static var sample: CarStatus {
        return CarStatus(
            connectionStatus: .connected,
            batteryLevel: 0.75,
            currentSpeed: 0.0,
            isRecording: false,
            isCameraActive: true,
            lastCommand: CarControlCommand(direction: .stop, speed: .stop),
            lastUpdated: Date()
        )
    }
} 